#!/bin/bash
##设置字体效果及相关打印函数
info='编写时间：2020-8-20
By：不愿意透漏姓名的男神
My_Gitee_Page：https://gitee.com/faith01238/
My_CSDN_Page：https://blog.csdn.net/qq_36154886
My_cnblogs_Page：https://www.cnblogs.com/liuyi778'
#设置镜像下载URL
cirros_url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
##定义字体效果
#红色背景
red_bg='\e[41;33;1m'
#红色字体
red_zt='\e[1;31m'
#绿色背景
green_bg='\033[0;32;1m'
#绿色字体
green_zt='\e[1;32m'
#关闭效果
end='\033[0m'
#字体闪烁
ss='\033[05m'
#消隐
sy='\33[08m'
##常用语句定义区
print_green_bg_red_zt(){
	echo -e "${green_bg}${ss}${green_bg}正在进入...${end}${end}${end}\n${red_zt}${1}...${end}"
}
print_red_z_green_z_info(){
	echo -e "${red_zt}${1}:${end}${green_zt}${2}${end}"
}
cheks(){
	echo -e "${red_zt}正在检测${1}...${end}"
}
print_green_zt(){
	echo -e "${green_zt}${1}${end}"
}
print_red_z_green_z(){
	echo -e "${red_zt}${1}${end}${green_zt}${2}${end}${green_zt}${3}${end}"
}
print_red_z(){
	echo -e "${red_zt}${1}${end}"
}
print_red_z_b(){
	echo -e "${red_bg}${1}${end}"
}
print_red_z_z_bg(){
	echo -e "${red_zt}${1}${end}${red_bg}${2}${end}"
}
input(){
	echo -e "${red_zt}${1}${end}"
	read $2
}
for i in $info;do
    print_green_zt "$i"
done
info_file='/install-info.sh'
if [[ -f "/usr/bin/yum" ]];then
    local_search='rpm -qa '
fi
if [[ -f "/etc/keystone/admin-openrc.sh" ]];then
    source /etc/keystone/admin-openrc.sh
fi
if [[ -f "/etc/keystone/demo-openrc.sh" ]];then
    source /etc/keystone/demo-openrc.sh
fi
if [[ -f "$info_file" ]];then
    source $info_file
    if [[ "$Successs" == 'y' || "$Successs" == 'Y' ]];then
        print_red_z_green_z_info "正在进入--->>>" "\t安装程序"
    else
        print_red_z_green_z "请使用下面的命令进行编辑文件,将信息录入完成之后，把'Successs='改成'Successs=y'" "\n vi ${info_file}\n然后再运行此脚本"
        exit 11
    fi
else
    print_red_z_green_z "已新建${info_file}文件，请使用下面的命令进行编辑文件,将信息录入完成之后，把'Successs='改成'Successs=y'" "\n vi ${info_file}\n再运行此脚本"
    echo '#!/bin/bash
##安装前手动设置安装信息
#请在=后面输入相关数据的值
#########此项目仅限于Centos7.x系统############################################
#######################################################信息录入区#########################################
#控制节点的IP地址
host_ip=10.1.1.12
#计算节点的IP地址
host_ip_node=10.1.1.4
#控制节点的主机名[建议${host_name}]
host_name=${host_name}
#计算节点的主机名[建议compute/1/2/3]
host_name_node=compute

###############################用户名及域名配置
#域名配置
DOMAIN_NAME=default

DOMAIN_NAME
###############################Openstack用户/密码设置
#open stack管理员[admin]密码
openstack_admin_pass=000000
#openstack用户[demo]的密码
openstack_user_pass=000000


###############################Openstack组件用户密码设置
keystone_pass=000000
glance_pass=000000
nova_pass=000000
neutron_pass=000000



#RABBIT的用户名[建议openstack]
rabbit_user=openstack
#RABBIT的用户密码[建议openstack]
rabbit_password=000000


################################数据库用户密码设置####################
##root 用户密码
DB_PASS=000000
##neutron 用户密码
neutron_db_pass=000000
##keystone 用户密码
keystone_db_pass=000000
##glance 用户密码
glance_db_pass=000000
##nova 用户密码
nova_db_pass=000000
nova_api_db_pass=000000

#服务通讯网卡名[将清除改网卡的IPV4功能],勿填写当前SSH连接所使用的网卡名
INTERFACE_NAME=ens33
#SWIFT服务的主机IP地址[可以不填写]
STORAGE_LOCAL_NET_IP=10.1.0.89
#SWIFT主机的SWIFT专用分区[可以不填写]
STORAGE_LOCAL_NET_FQ=sdap4
#########################################################信息录入区#########################################
####################录入完成确认区##########################
##如编辑完成请在下面的=输入任意字符串[y/n],此变量将作为唯一验证信息录入的依据
Successs=n
##非常重要####
###################是否显示服务安装/卸载过程#########################
display=n  #默认使用[n],可选项[y/n]' > $info_file
    exit 10
fi
install_check(){
    print_red_z_green_z_info "安装配置信息确认"
    input "当前主机名预配置为: $host_name\n按回车继续安装" "dd"
}
chek_status(){
    if [[ "$1" -eq '0' ]];then
        print_red_z_green_z_info "$2" "成功"
        return 0
    else
        status='true'
        while $status;do
            print_red_z_z_bg "$2" "失败"
            err='n'
            input "是否停止脚本运行[当前脚本PID: $$]\n输入f忽略该提示" "err"
            if [[ "$err" == 'f' ]];then
                status='false'
                if [[ "$status" == 'false' ]];then
                    print_red_z "已跳过该错误:[${2}失败]"
                else
                    status='true'
                fi
            else
                status=true
            fi
        done
    fi
}
#服务循环安装
if [[ "$display" == 'y' || "$display" == 'Y' ]];then
    export install_s='yum install -y'
    export unstall_s='yum remove -y '
else
    export install_s='yum install -y -q'
    export unstall_s='yum remove -y -q'
fi
service_install(){
    for i in $*;do
        print_red_z_green_z_info "正在安装---->>>>" "${i}\t安装速度根据包的大小及网络而定,请耐心等待"
        print_red_z_green_z_info "当前脚本进程PID---->>>" "$$"
        $install_s $i
        chek_status "$?" "${i}安装"
    done
}
#服务卸载
service_unstall(){
    input "即将卸载以下服务:\n$* \n是否全部卸载[y/n]" "re"
    for i in $*;do
        print_green_zt "$i"
    done
    if [[ "$re" == "y" || "$re" == "Y" ]];then
        for i in $*;do
            print_red_z_green_z_info "正在卸载---->>>>" "$i"
            $unstall_s $i
            chek_status "$?" "${i}卸载"
        done
    else
        input "是否自定义输入卸载[y/n]" "zre"
        if  [[ "$zre" == "y" || "$zre" == "Y" ]];then
            input "请输入需要卸载的包,并以空格分开" "sre"
            for i in $sre;do
                $unstall_s $i
            done
        else
            print_red_z "跳过卸载"
        fi
    fi
}
#服务启动
service_start(){
    systemctl daemon-reload
    for i in $*;do
        systemctl restart $i
        chek_status "$?" "${i}服务启动"
        systemctl enable $i > /dev/null
        chek_status "$?" "${i}设置服务开机启动"
    done
}
service_stop(){
    for i in $*;do
        systemctl stop $i
        chek_status "$?" "${i}服务关闭"
        systemctl disable $i > /dev/null
        chek_status "$?" "${i}关闭开机启动"
    done
}
service_chek(){
    print_red_z_green_z_info "正在检查" "$1"
    ser=`$local_search | grep $1`
    if [[ -n "$ser" ]];then
        input "检测到本机已安装[$1]相关服务,请选择一个操作\n[1]跳过此服务[默认选项]\n[2]卸载该服务" db_remove
        if [[ "$db_remove" == '2' ]];then
            service=`$local_search | grep $1`
            service_unstall "$service"
        else
            print_red_z_green_z_info "跳过卸载" "$service"
            db_init='y'
        fi
    fi
}
cmd(){
    for i in $*;do
        print_red_z_green_z_info "正在执行" "$i"
        $i
        chek_status "$1" "执行"
    done
}
if [[ "$USER" -eq 'ROOT' ]];then
    print_green_zt "正在使用${USER}用户"
else
    print_red_z_b "请使用root用户"
fi
yum_clean(){
    yum clean all > /dev/null
    chek_status "$?" "$1"
}
yum_config(){
    rm -rf /etc/yum.repos.d/*
    mkdir -p /etc/yum.repos.d/
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
    yum_clean "系统源配置"
    echo '[centos-openstack-queens]
name=CentOS-7 - OpenStack queens
baseurl=http://mirrors.tuna.tsinghua.edu.cn/$contentdir/$releasever/cloud/$basearch/openstack-queens/
gpgcheck=0
enabled=1' > /etc/yum.repos.d/tsinghua-OpenStack-queens.repo
    yum_clean "清华OpenStack-queens源配置"
    echo '[centos-qemu-ev]
name=CentOS-$releasever - QEMU EV
#mirrorlist=http://mirrors.tuna.tsinghua.edu.cn/?release=$releasever&arch=$basearch&repo=virt-kvm-common
baseurl=http://mirrors.tuna.tsinghua.edu.cn/$contentdir/$releasever/virt/$basearch/kvm-common/
gpgcheck=0
enabled=1' > /etc/yum.repos.d/tsinghua-QEMU-EV.repo
    yum_clean "清华qemu-ev源配置"
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum_clean "eprl源配置"
#     echo '[mongodb-org] 
# name = MongoDB Repository
# baseurl = https://mirrors.aliyun.com/mongodb/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
# gpgcheck = 1 
# enabled = 1 
# gpgkey = https：// www.mongodb.org/static/pgp/server-3.6.asc' > /etc/yum.repos.d/mongod.repo
#     echo '[centos-storage-debuginfo]
# name=CentOS-$releasever - Storage SIG - debuginfo
# baseurl=http://debuginfo.centos.org/$contentdir/$releasever/storage/$basearch/
# gpgcheck=0
# enabled=1' > /etc/yum.repos.d/tsinghua-storage.repo
#     yum_clean "Storage源配置"
    # if [[ `yum repolist | wc -l` -eq '10' ]];then
    #     print_red_z_green_z_info "所有源配置" "成功"
    # fi
    print_red_z_green_z_info "正在进行" "源索引创建"
    print_red_z_green_z_info "当前脚本进程PID---->>>" "$$"
    # mv  /yum_bak/* /etc/yum.repos.d/
    yum_clean "所有源配置"
    print_red_z_green_z_info "正在进行-->>" "源索引重建,请耐心等待"
    yum makecache
    chek_status "$?" "索引更新"
    print_red_z_green_z_info "正在进行-->>" "软件更新，请耐心等待"
    yum upgrade
    chek_status "$?" "软件升级"
}
config_sys(){
    print_red_z_green_z_info "正在进入" "系统环境配置"
    print_red_z_green_z_info "正在进行" "selinux配置"
    setenforce 0
    if [[ -f "/etc/selinux/config" ]];then
            sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
    fi
    if [[ -f "/etc/sysconfig/selinux" ]];then
            sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
    fi
    echo -e "net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1\nnet.ipv4.ip_forward = 1" > /etc/sysctl.conf
    systemctl stop firewalld NetworkManager.service
    service="firewalld NetworkManager.service"
    service_unstall "$service"
    service="ntp  iptables"
    service_install "$service"
	service_start "iptables"
    print_red_z_green_z_info "正在进行" "网络时间同步"
    ntpdate time.windows.com
    chek_status "$?" "第一次时间同步"
    ntpdate time.windows.com
    chek_status "$?" "第二次时间同步"
    ntpdate time.windows.com
    chek_status "$?" "第三次时间同步"
	iptables -F
	iptables -X
	iptables -X
    service iptables save
    if [[ `hostname -I |grep  $HOST_IP ` != '' ]];then 
		hostnamectl set-hostname $host_name
		print_red_z_green_z_info "已配置该主机名为:" "$host_name"
	elif [[ `hostname -I |grep  $HOST_IP_NODE ` != '' ]];then 
		print_red_z_green_z_info "已配置该主机名为" "$host_name_NODE"
		hostnamectl set-hostname $host_name_NODE
	else
		input '无法判断此节点类型，请输入该节点需要配置的主机名' hname
		hostnamectl set-hostname $hname
		print_red_z_green_z_info "已配置该主机名为" "$hname"
	fi
    echo -e "$HOST_IP $host_name" >> /etc/hosts
	echo -e "$HOST_IP_NODE $host_name_NODE" >> /etc/hosts
    sed -i -e 's/#UseDNS yes/UseDNS no/g' -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
    service="openstack-selinux python-openstackclient crudini"
    service_install "$service"
    print_red_z_b "系统配置完成，倒计时重启系统"
    echo 'config=y' >> $info_file
    count=20
    while true;do
        if [ $count -eq 1 ];then
            reboot
        else
            let count--;
            sleep 1;
            tput rc
            tput ed
            print_red_z_green_z_info "倒计时" "$count"
        fi
    done
}
init_mariadb(){
    service_start "mariadb"
    mysql "use mysql;grant all PRIVILEGES on *.* to 'root'@'localhost' IDENTIFIED BY '$DB_PASS';"
    chek_status "$?" "数据库初始化"
    mysql_cmd "drop database test;"
    chek_status "$?" "删除测试数据库"
    mysql_cmd "use mysql;grant all PRIVILEGES on *.* to 'root'@'%' IDENTIFIED BY '$DB_PASS';"
    chek_status "$?" "数据库远程登录配置"
}
chrony_install(){
    ser='chrony'
    service_install "$ser"
    sed -i 's/^server/#server/g' /etc/chrony.conf
    echo -e "server $host_name iburst\nallow 0.0.0.0/0" >> /etc/chrony.conf
    service_start "chronyd"
}
mongodb_install(){
    ser='mongodb-server mongodb'
    service_install "$ser"
    sed -i "s#bind_ip.*#bind_ip = $host_ip#" /etc/mongod.conf
    sed -i 's/#smallfiles/smallfiles/g' /etc/mongod.conf
    service_start "mongod.service"
}
rabbitmq_install(){
    service='python2-PyMySQL rabbitmq-server  ntp'
    service_install "$service"
    service='rabbitmq-server.service'
    service_start "$service"
    rabbitmqctl add_user $rabbit_user $ALL_PASS
    chek_status "$?" "rabbitmqctl创建用户:$rabbit_user"
    rabbitmqctl set_permissions $rabbit_user ".*" ".*" ".*"
    chek_status "$?" "rabbitmqctl用户授权:$rabbit_user"
}
memcached_install(){
    ser='memcached python-memcached'
    service_install "$ser"
    service_start "memcached.service"
}
mariadb_install(){
    print_red_z_green_z_info "正在进入" "mariadb服务安装"
    service_chek "mariadb-server"
    service="mariadb-server MySQL-python expect"
    service_install "$service"
    # crudini --set /usr/lib/systemd/system/mariadb.service Service LimitNOFILE 10000
    # crudini --set /usr/lib/systemd/system/mariadb.service Service LimitNPROC 10000
    echo -e "bind-address = $host_ip
default-storage-engine = innodb
innodb_file_per_table
max_connections = 4096
collation-server = utf8_general_ci
character-set-server = utf8" > /etc/my.cnf.d/openstack.cnf
    systemctl daemon-reload
    if [[ "$db_init" == 'y' ]];then
        ini='n' #告诉后面的步骤本机的数据库服务已经不是第一次安装了
        input "请输入本机数据库服务密码" "DB_PASS"
        if [[ -z "$DB_PASS" ]];then
            chek_status "2" "数据库密码输入"
        else
            mysql -uroot -p$DB_PASS -e "show databases;"
            chek_status "$?" "数据库登录"
            export ALL_PASS=$DB_PASS
        fi
    else
        print_green_zt "全新安装"
    fi
    print_red_z_green_z_info "正在启动服务" "mariadb"
    service_start "mariadb"
    print_green_zt ' --------初始化数据库------------'
    if [[ "$ini" == "n" ]];then
        print_green_zt "正在进行强制修改数据库密码"
        print_red_z_green_z_info "请打开下面的链接，根据教程重置数据库密码,记得点个关注喔" "https://blog.csdn.net/qq_36154886/article/details/106109417"
        chek_status "11"
        input "请输入当前数据库root用户登录密码" DB_PASS
        mysql -uroot -p$DB_PASS -e "use mysql;show tables;"
        chek_status "$?" "新密码登录验证"
        if [[ "$?" -eq '0' ]];then
            ALL_PASS=$DB_PASS
        fi
    else
        init_mariadb
    fi
    mysql -uroot -p$ALL_PASS -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$ALL_PASS';"
    chek_status "$?" "数据库登录"
}
mysql_cmd(){
    cmd="$1"
    mysql -uroot -p$DB_PASS -e "$cmd"
    if [[ "$?" -eq '0' ]];then
        return 0
    else
        return 404
    fi
}
database_create(){
    mysql_cmd "GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY '$keystone_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' IDENTIFIED BY '$keystone_db_pass';"
    chek_status "$?" "数据库[keystone]创建"
    mysql_cmd "GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' IDENTIFIED BY '$glance_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' IDENTIFIED BY '$glance_db_pass';"
    chek_status "$?" "数据库[glance]创建"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'localhost' IDENTIFIED BY '$nova_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'%' IDENTIFIED BY '$nova_db_pass';"
    chek_status "$?" "数据库[nova]创建"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova_api.* TO 'nova_api'@'localhost' IDENTIFIED BY '$nova_api_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova_api.* TO 'nova_api'@'%' IDENTIFIED BY '$nova_api_db_pass';"
    chek_status "$?" "数据库[nova_api]创建"
    mysql_cmd "GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'localhost' IDENTIFIED BY '$neutron_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'%' IDENTIFIED BY '$neutron_db_pass';"
    chek_status "$?" "数据库[neutron]创建"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova.* to 'nova_api'@'%' IDENTIFIED BY '$nova_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova.* to 'nova_api'@'%' IDENTIFIED BY '$nova_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova_api.* to 'nova'@'%' IDENTIFIED BY '$nova_api_db_pass';"
    mysql_cmd "GRANT ALL PRIVILEGES ON nova_api.* to 'nova'@'%' IDENTIFIED BY '$nova_api_db_pass';"
    mysql_cmd "flush privileges;"
    chek_status "$?" "所有数据库创建"
}
keystone_install(){
    print_red_z_green_z_info "正在进入" "keystone服务安装"
    service='openstack-selinux python-openstackclient openstack-keystone httpd mod_wsgi'
    service_install "$service"
    sed -i  "/^symbolic-links/a\default-storage-engine = innodb\ninnodb_file_per_table\ncollation-server = utf8_general_ci\ninit-connect = 'SET NAMES utf8'\ncharacter-set-server = utf8\nmax_connections=10000" /etc/my.cnf
    ADMIN_TOKEN=$(openssl rand -hex 10)
    crudini --set /etc/keystone/keystone.conf DEFAULT admin_token $ADMIN_TOKEN
    crudini --set /etc/keystone/keystone.conf token provider  fernet
    crudini --set /etc/keystone/keystone.conf database connection  mysql+pymysql://keystone:${keystone_db_pass}@${host_name}/keystone
    su -s /bin/sh -c "keystone-manage db_sync" keystone
    chek_status "$?" "数据库初始化"
    keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
    sed -i "s/#ServerName.*/ServerName $host_name/g" /etc/httpd/conf/httpd.conf 
cat >/etc/httpd/conf.d/wsgi-keystone.conf<<- EOF
Listen 5000
Listen 35357

<VirtualHost *:5000>
    WSGIDaemonProcess keystone-public processes=5 threads=1 user=keystone group=keystone display-name=%{GROUP}
    WSGIProcessGroup keystone-public
    WSGIScriptAlias / /usr/bin/keystone-wsgi-public
    WSGIApplicationGroup %{GLOBAL}
    WSGIPassAuthorization On
    ErrorLogFormat "%{cu}t %M"
    ErrorLog /var/log/httpd/keystone-error.log
    CustomLog /var/log/httpd/keystone-access.log combined

    <Directory /usr/bin>
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:35357>
    WSGIDaemonProcess keystone-admin processes=5 threads=1 user=keystone group=keystone display-name=%{GROUP}
    WSGIProcessGroup keystone-admin
    WSGIScriptAlias / /usr/bin/keystone-wsgi-admin
    WSGIApplicationGroup %{GLOBAL}
    WSGIPassAuthorization On
    ErrorLogFormat "%{cu}t %M"
    ErrorLog /var/log/httpd/keystone-error.log
    CustomLog /var/log/httpd/keystone-access.log combined

    <Directory /usr/bin>
        Require all granted
    </Directory>
</VirtualHost>
EOF
    service_start "httpd"
    export OS_TOKEN=$ADMIN_TOKEN
    export OS_URL=http://${host_name}:35357/v3
    export OS_IDENTITY_API_VERSION=3
    openstack service create --name keystone --description "OpenStack Identity" identity

    chek_status "$?" "openstack创建服务:keystone"
    openstack endpoint create --region RegionOne identity public http://$host_name:5000/v3 
    chek_status "$?" "openstack创建端点:public"
    openstack endpoint create --region RegionOne identity internal http://$host_name:5000/v3
    chek_status "$?" "openstack创建端点:internal"
    openstack endpoint create --region RegionOne identity admin http://$host_name:35357/v3
    chek_status "$?" "openstack创建端点:admin"

    #################################管理域固定创建区
    openstack domain create --description "Default Domain" $DOMAIN_NAME
    chek_status "$?" "创建默认域[default]"
    openstack project create --domain default --description "Admin Project" admin
    chek_status "$?" "创建管理项目[admin]"
    openstack user create --domain default --password $openstack_admin_pass admin
    chek_status "$?" "创建用户[admin]"
    openstack role create admin
    chek_status "$?" "创建角色[admin]"
    openstack role add --project admin --user admin admin
    chek_status "$?" "将角色[admin]与用户进行绑定[admin]"


    ###############################服务域(service)固定创建区
    openstack project create --domain default --description "Service Project" service
    chek_status "$?" "创建用户域[service]"
    openstack project create --domain default --description "Demo Project" demo
    chek_status "$?" "创建项目[demo]"
    openstack user create --domain default --password $openstack_user_pass demo
    chek_status "$?" "创建用户[demo]"
    openstack role create user
    chek_status "$?" "创建角色[user]"
    openstack role add --project demo --user demo user
    chek_status "$?" "将角色[user]与用户[demo]进行绑定"

    unset OS_TOKEN OS_URL

    cat > /etc/keystone/admin-openrc.sh <<-EOF
export OS_PROJECT_DOMAIN_NAME=$DOMAIN_NAME
export OS_USER_DOMAIN_NAME=$DOMAIN_NAME
export OS_PROJECT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=$openstack_pass
export OS_AUTH_URL=http://${host_name}:35357/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2
EOF
cat > /etc/keystone/demo-openrc.sh <<-EOF
export OS_PROJECT_DOMAIN_NAME=default
export OS_USER_DOMAIN_NAME=default
export OS_PROJECT_NAME=demo
export OS_USERNAME=demo
export OS_PASSWORD=$openstack_user_pass
export OS_AUTH_URL=http://${host_name}:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2
EOF
    source /etc/keystone/admin-openrc.sh
    source /etc/keystone/demo-openrc.sh
    unset OS_TOKEN OS_URL
    openstack --os-auth-url http://${host_name}:35357/v3 --os-project-domain-name default --os-user-domain-name default --os-project-name admin --os-username admin token issue
    chek_status "$?" "admin用户请求令牌"
    openstack --os-auth-url http://${host_name}:5000/v3 --os-project-domain-name default --os-user-domain-name default  --os-project-name demo --os-username demo token issue
    chek_status "$?" "deme用户请求令牌"
    source /etc/keystone/admin-openrc.sh
    source /etc/keystone/demo-openrc.sh
    openstack token issue
    chek_status "$?" "令牌生成"
    echo  "Keystone安装完成" >> /install_logs.log
    print_green_zt "Keystone安装完成"
}
glance_install(){
    print_red_z_green_z_info "正在进入" "glance服务安装"
    service_install "openstack-glance"
    source /etc/keystone/admin-openrc.sh
    openstack user create --domain default --password $glance_pass glance
    chek_status "$?" "openstack创建用户: glance"
    openstack role add --project service --user glance admin
    chek_status "$?" "openstack添加glance用户到admin角色"
    openstack service create --name glance --description "OpenStack Image" image
    chek_status "$?" "openstack创建服务:keystone"

    openstack endpoint create --region RegionOne image public http://$host_name:9292
    chek_status "$?" "openstack创建image端点: public"
    openstack endpoint create --region RegionOne image internal http://$host_name:9292
    chek_status "$?" "openstack创建image端点: internal"
    openstack endpoint create --region RegionOne image admin http://$host_name:9292
    chek_status "$?" "openstack创建image端点: admin"


    crudini --set /etc/glance/glance-api.conf database connection  mysql+pymysql://glance:${glance_db_pass}@${host_name}/glance
    crudini --set /etc/glance/glance-api.conf keystone_authtoken auth_uri http://${host_name}:5000
    crudini --set /etc/glance/glance-api.conf keystone_authtoken auth_url http://${host_name}:35357
    crudini --set /etc/glance/glance-api.conf keystone_authtoken memcached_servers  ${host_name}:11211
    crudini --set /etc/glance/glance-api.conf keystone_authtoken auth_type password
    crudini --set /etc/glance/glance-api.conf keystone_authtoken project_domain_name default
    crudini --set /etc/glance/glance-api.conf keystone_authtoken user_domain_name default
    crudini --set /etc/glance/glance-api.conf keystone_authtoken project_name service
    crudini --set /etc/glance/glance-api.conf keystone_authtoken username glance
    crudini --set /etc/glance/glance-api.conf keystone_authtoken password $glance_pass
    crudini --set /etc/glance/glance-api.conf paste_deploy flavor keystone
    # crudini --set /etc/glance/glance-api.conf paste_deploy config_file  /usr/share/glance/glance-api-dist-paste.ini
    crudini --set /etc/glance/glance-api.conf glance_store stores file,http
    crudini --set /etc/glance/glance-api.conf glance_store default_store file
    crudini --set /etc/glance/glance-api.conf glance_store filesystem_store_datadir /var/lib/glance/images/

    crudini --set /etc/glance/glance-registry.conf database connection  mysql+pymysql://glance:${glance_db_pass}@$host_name/glance
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken auth_uri http://$host_name:5000
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken auth_url http://$host_name:35357
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken memcached_servers $host_name:11211
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken auth_type password
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken project_domain_name default
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken user_domain_name default
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken project_name service
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken username glance
    crudini --set /etc/glance/glance-registry.conf keystone_authtoken password $glance_pass
    crudini --set /etc/glance/glance-registry.conf paste_deploy flavor keystone
    crudini --set /etc/glance/glance-registry.conf paste_deploy config_file  /usr/share/glance/glance-registry-dist-paste.ini

    su -s /bin/sh -c "glance-manage db_sync" glance
    chek_status "$?" "glacne数据库初始化"
    mysql -uglance -p${glance_db_pass} -e "use glance;select * from glance.alembic_version;"
    service='openstack-glance-api.service openstack-glance-registry.service'
    service_start "$service"
    source /etc/keystone/admin-openrc.sh
    if [[ ! -f "cirros-0.3.4-x86_64-disk.img" ]];then
        if [[  ! -f "/usr/bin/wget" ]];then
            service_install "wget"
        fi
        wget $cirros_url
        openstack image create "cirros"   --file cirros-0.3.4-x86_64-disk.img   --disk-format qcow2 --container-format bare   --public
        chek_status  "$?" "镜像上传测试"
    else
        openstack image create "cirros"   --file cirros-0.3.4-x86_64-disk.img   --disk-format qcow2 --container-format bare   --public
        chek_status  "$?" "镜像上传测试"
    fi
    openstack image list
    chek_status "$?" "镜像保存"
    echo  "Glance安装完成" >> /install_logs.log
    print_green_zt "Glance安装完成"
}
nova_install(){
    print_red_z_green_z_info "正在进入" "nova服务安装"
    openstack user create --domain default --password $nova_pass nova
    chek_status "$?" "openstack创建用户:  nova"
    openstack role add --project service --user nova admin
    chek_status "$?" "openstack添加nova用户到admin角色"
    openstack service create --name nova --description "OpenStack Compute" compute
    chek_status "$?" "openstack创建服务: nova"
    openstack endpoint create --region RegionOne compute public http://$host_name:8774/v2.1/%\(tenant_id\)s
    chek_status "$?" "openstack创建nova端点: public"
    openstack endpoint create --region RegionOne compute internal http://$host_name:8774/v2.1/%\(tenant_id\)s
    chek_status "$?" "openstack创建nova端点: internal"
    openstack endpoint create --region RegionOne compute admin http://$host_name:8774/v2.1/%\(tenant_id\)s
    chek_status "$?" "openstack创建nova端点: compute"
    service='openstack-nova-api openstack-nova-conductor openstack-nova-console openstack-nova-novncproxy openstack-nova-scheduler'
    service_install "$service"
    crudini --set /etc/nova/nova.conf database connection mysql+pymysql://nova:${nova_db_pass}@${host_name}/nova
    crudini --set /etc/nova/nova.conf api_database connection mysql+pymysql://nova:${nova_db_pass}@${host_name}/nova_api

    crudini --set /etc/nova/nova.conf DEFAULT enabled_apis  osapi_compute,metadata
    crudini --set /etc/nova/nova.conf DEFAULT rpc_backend  rabbit
    crudini --set /etc/nova/nova.conf DEFAULT auth_strategy   keystone
    crudini --set /etc/nova/nova.conf DEFAULT my_ip $HOST_IP
    crudini --set /etc/nova/nova.conf DEFAULT use_neutron True
    crudini --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver
    crudini --set /etc/nova/nova.conf DEFAULT metadata_listen 0.0.0.0 
    crudini --set /etc/nova/nova.conf DEFAULT metadata_listen_port 8775

    crudini --set /etc/nova/nova.conf oslo_messaging_rabbit rabbit_host ${host_name}
    crudini --set /etc/nova/nova.conf oslo_messaging_rabbit rabbit_userid $rabbit_user
    crudini --set /etc/nova/nova.conf oslo_messaging_rabbit  rabbit_password  $rabbit_password

    crudini --set /etc/nova/nova.conf keystone_authtoken auth_uri http://${host_name}:5000
    crudini --set /etc/nova/nova.conf keystone_authtoken auth_url http://${host_name}:35357
    crudini --set /etc/nova/nova.conf keystone_authtoken memcached_servers ${host_name}:11211
    crudini --set /etc/nova/nova.conf keystone_authtoken auth_type password
    crudini --set /etc/nova/nova.conf keystone_authtoken project_domain_name default
    crudini --set /etc/nova/nova.conf keystone_authtoken user_domain_name default
    crudini --set /etc/nova/nova.conf keystone_authtoken project_name service
    crudini --set /etc/nova/nova.conf keystone_authtoken username  nova
    crudini --set /etc/nova/nova.conf keystone_authtoken password ${nova_pass}

    crudini --set /etc/nova/nova.conf vnc vncserver_listen ${HOST_IP}
    crudini --set /etc/nova/nova.conf vnc vncserver_proxyclient_address ${HOST_IP}

    crudini --set /etc/nova/nova.conf glance api_servers http://${host_name}:9292

    crudini --set /etc/nova/nova.conf oslo_concurrency lock_path /var/lib/nova/tmp
    su -s /bin/sh -c "nova-manage api_db sync" nova
    su -s /bin/sh -c "nova-manage db sync" nova
    iptables -F
    iptables -X
    iptables -Z
    /usr/libexec/iptables/iptables.init save
    service='openstack-nova-api.service  openstack-nova-consoleauth.service openstack-nova-scheduler.service openstack-nova-conductor.service openstack-nova-novncproxy.service'
    service_start "$service"
    echo "Nova服务安装完成" >> /install_logs.log
    print_red_z_green_z_info "nova服务安装" "完成"
}
nova_compute(){
    se='openstack-nova-compute'
    service_install "$se"
    crudini --set /etc/nova/nova.conf DEFAULT rpc_backend  rabbit
    crudini --set /etc/nova/nova.conf DEFAULT rabbit_host $host_name
    crudini --set /etc/nova/nova.conf DEFAULT rabbit_userid $rabbit_user
    crudini --set /etc/nova/nova.conf DEFAULT rabbit_password $rabbit_password
    crudini --set /etc/nova/nova.conf DEFAULT auth_strategy keystone
    crudini --set /etc/nova/nova.conf keystone_authtoken auth_uri http://$host_name:5000
    crudini --set /etc/nova/nova.conf keystone_authtoken auth_url  http://$host_name:35357
    crudini --set /etc/nova/nova.conf keystone_authtoken memcached_servers $host_name::11211
    crudini --set /etc/nova/nova.conf keystone_authtoken auth_type password
    crudini --set /etc/nova/nova.conf keystone_authtoken project_domain_name $domain_name
    crudini --set /etc/nova/nova.conf keystone_authtoken user_domain_name $domain_name
    crudini --set /etc/nova/nova.conf keystone_authtoken project_name $domain_name
    crudini --set /etc/nova/nova.conf keystone_authtoken username nova
    crudini --set /etc/nova/nova.conf keystone_authtoken password = $ALL_PASS
    crudini --set /etc/nova/nova.conf DEFAULT my_ip $HOST_IP_NODE
    crudini --set /etc/nova/nova.conf DEFAULT use_neutron True
    crudini --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver
    crudini --set /etc/nova/nova.conf vnc enabled True
    crudini --set /etc/nova/nova.conf vnc vncserver_listen 0.0.0.0
    crudini --set /etc/nova/nova.conf vnc vncserver_proxyclient_address $HOST_IP_NODE
    crudini --set /etc/nova/nova.conf vnc novncproxy_base_url http://:6080/vnc_auto.html
    crudini --set /etc/nova/nova.conf glance api_servers http://${HOST_NAME}:9292
    crudini --set /etc/nova/nova.conf oslo_concurrency lock_path /var/lib/nova/tmp
    i=`egrep -c '(vmx|svm)' /proc/cpuinfo`
    if [[ "$i" == '0' ]];then
        crudini --set /etc/nova/nova.conf libvirt virt_type qemu
    fi
    ser='libvirtd.service openstack-nova-compute.service'
    service_start "$ser"
    echo "Nova-Compute服务安装完成" >> /install_logs.log
}
neutron_install(){
    print_red_z_green_z_info "正在进入" "neutron服务安装"
    source /etc/keystone/admin-openrc.sh
    openstack user create --domain $domain_name --password $ALL_PASS neutron
    openstack role add --project service --user neutron admin
    openstack service create --name neutron --description "OpenStack Networking" network
    openstack endpoint create --region RegionOne network public http://$host_name:9696
    openstack endpoint create --region RegionOne network internal http://$host_name:9696
    openstack endpoint create --region RegionOne network admin http://$host_name:9696
    service='openstack-neutron openstack-neutron-ml2 openstack-neutron-linuxbridge ebtables  openstack-neutron-openvswitch  openstack-neutron-lbaas python-neutron-lbaas haproxy  openstack-neutron-fwaas'
    service_install "$service"
    crudini --set /etc/neutron/neutron.conf database connection  mysql://neutron:${neutron_db_pass}@${host_name}/neutron
    crudini --set /etc/neutron/neutron.conf DEFAULT rpc_backend rabbit
    crudini --set /etc/neutron/neutron.conf oslo_messaging_rabbit rabbit_host ${host_name}
    crudini --set /etc/neutron/neutron.conf oslo_messaging_rabbit rabbit_userid openstack
    crudini --set /etc/neutron/neutron.conf oslo_messaging_rabbit password  ${neutron_pass}

    crudini --set /etc/neutron/neutron.conf DEFAULT core_plugin  ml2
    crudini --set /etc/neutron/neutron.conf DEFAULT service_plugins  router
    crudini --set /etc/neutron/neutron.conf DEFAULT allow_overlapping_ips  True

    crudini --set /etc/neutron/neutron.conf DEFAULT auth_strategy keystone
    crudini --set /etc/neutron/neutron.conf keystone_authtoken auth_uri  http://${host_name}:5000
    crudini --set /etc/neutron/neutron.conf keystone_authtoken auth_url  http://${host_name}:35357
    crudini --set /etc/neutron/neutron.conf keystone_authtoken memcached_servers  ${host_name}:11211
    crudini --set /etc/neutron/neutron.conf keystone_authtoken auth_type  password
    crudini --set /etc/neutron/neutron.conf keystone_authtoken project_domain_name  default
    crudini --set /etc/neutron/neutron.conf keystone_authtoken user_domain_name default
    crudini --set /etc/neutron/neutron.conf keystone_authtoken project_name  service
    crudini --set /etc/neutron/neutron.conf keystone_authtoken username  neutron
    crudini --set /etc/neutron/neutron.conf keystone_authtoken password  ${neutron_pass}

    crudini --set /etc/neutron/neutron.conf DEFAULT notify_nova_on_port_status_changes  True
    crudini --set /etc/neutron/neutron.conf DEFAULT notify_nova_on_port_data_changes  True
    crudini --set /etc/neutron/neutron.conf  nova auth_url  http://${host_name}:35357
    crudini --set /etc/neutron/neutron.conf  nova auth_type  password
    crudini --set /etc/neutron/neutron.conf  nova project_domain_name  default
    crudini --set /etc/neutron/neutron.conf  nova user_domain_name  default
    crudini --set /etc/neutron/neutron.conf  nova region_name  RegionOne
    crudini --set /etc/neutron/neutron.conf  nova project_name  service
    crudini --set /etc/neutron/neutron.conf  nova username  nova
    crudini --set /etc/neutron/neutron.conf  nova password  ${nova_pass}
    crudini --set /etc/neutron/neutron.conf oslo_concurrency lock_path  /var/lib/neutron/tmp

    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 type_drivers  flat,vlan,gre,vxlan,local
    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types  
    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 mechanism_drivers  openvswitch,l2population
    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 extension_drivers  port_security

    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini securitygroup enable_ipset  True
    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini securitygroup enable_security_group true
    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini securitygroup firewall_driver iptables_hybrid

    crudini --set  /etc/neutron/l3_agent.ini DEFAULT interface_driver  neutron.agent.linux.interface.OVSInterfaceDriver
    crudini --set  /etc/neutron/l3_agent.ini DEFAULT external_network_bridge 

    crudini --set  /etc/neutron/dhcp_agent.ini DEFAULT interface_driver  neutron.agent.linux.interface.OVSInterfaceDriver
    crudini --set  /etc/neutron/dhcp_agent.ini DEFAULT dhcp_driver  neutron.agent.linux.dhcp.Dnsmasq
    crudini --set  /etc/neutron/dhcp_agent.ini DEFAULT enable_isolated_metadata  True

    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini agent l2_population True
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini agent prevent_arp_spoofing True
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs integration_bridge br-int
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini securitygroup  firewall_driver iptables_hybrid

    crudini --set  /etc/neutron/metadata_agent.ini DEFAULT nova_metadata_ip  $HOST_IP
    crudini --set  /etc/neutron/metadata_agent.ini DEFAULT metadata_proxy_shared_secret  $ALL_PASS
    # crudini --set  /etc/neutron/metadata_agent.ini DEFAULT nova_metadata_port 8775

    crudini --set  /etc/nova/nova.conf DEFAULT auto_assign_floating_ip True
    crudini --set  /etc/nova/nova.conf DEFAULT metadata_listen 0.0.0.0
    crudini --set  /etc/nova/nova.conf DEFAULT metadata_listen_port 8775
    crudini --set  /etc/nova/nova.conf DEFAULT scheduler_default_filters 'AvailabilityZoneFilter,RamFilter,ComputeFilter,ComputeCapabilitiesFilter,ImagePropertiesFilter,ServerGroupAntiAffinityFilter,ServerGroupAffinityFilter'
    crudini --set  /etc/nova/nova.conf DEFAULT compute_driver libvirt.LibvirtDriver


    crudini --set  /etc/nova/nova.conf neutron  url  http://${host_name}:9696
    crudini --set  /etc/nova/nova.conf neutron  auth_url  http://${host_name}:35357
    crudini --set  /etc/nova/nova.conf neutron  auth_type  password
    crudini --set  /etc/nova/nova.conf neutron  project_domain_name  default
    crudini --set  /etc/nova/nova.conf neutron  user_domain_name  default
    crudini --set  /etc/nova/nova.conf neutron  region_name  RegionOne
    crudini --set  /etc/nova/nova.conf neutron  project_name  service
    crudini --set  /etc/nova/nova.conf neutron  username  neutron
    crudini --set  /etc/nova/nova.conf neutron  password  ${neutron_pass}
    crudini --set  /etc/nova/nova.conf neutron  service_metadata_proxy  True
    crudini --set  /etc/nova/nova.conf neutron  metadata_proxy_shared_secret  ${neutron_pass}

    echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf 
    echo "net.ipv4.conf.default.rp_filter=0" >> /etc/sysctl.conf 
    echo 'net.ipv4.conf.all.rp_filter=0' >> /etc/sysctl.conf
    sysctl -p 
    ln -s /etc/neutron/plugins/ml2/ml2_conf.ini /etc/neutron/plugin.ini
    su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neutron.conf   --config-file /etc/neutron/plugins/ml2/ml2_conf.ini upgrade head" neutron
    service_start "openvswitch"
    ovs-vsctl add-br br-int 
    chek_status  "$?" "Linux网桥初始化"
    ser='openstack-nova-api neutron-server neutron-openvswitch-agent.service neutron-dhcp-agent.service neutron-metadata-agent.service neutron-l3-agent.service'
    service_start "$ser"
    print_red_z_green_z_info "neutron服务安装" "已完成" 
    echo "Neutron服务安装完成" >> /install_logs.log
}
neutron_${host_name}_gre(){
    crudini --set /etc/neutron/neutron.conf DEFAULT service_plugins router,lbaas,firewall
    crudini --set /etc/neutron/neutron_lbaas.conf service_providers service_provider LOADBALANCER:Haproxy:neutron_lbaas.services.loadbalancer.drivers.haproxy.plugin_driver.HaproxyOnHostPluginDriver:default
    crudini --set /etc/neutron/lbaas_agent.ini DEFAULT device_driver neutron_lbaas.services.loadbalancer.drivers.haproxy.namespace_driver.HaproxyNSDriver
    crudini --set /etc/neutron/lbaas_agent.ini DEFAULT interface_driver neutron.agent.linux.interface.OVSInterfaceDriver
    crudini --set /etc/neutron/lbaas_agent.ini haproxy user_group haproxy

    crudini --set /etc/neutron/neutron.conf service_providers FIREWALL:Iptables:neutron.agent.linux.iptables_firewall.OVSHybridIptablesFirewallDriver:default
    crudini --set /etc/neutron/fwaas_driver.ini fwaas driver neutron_fwaas.services.firewall.drivers.linux.iptables_fwaas.IptablesFwaasDriver
    crudini --set /etc/neutron/fwaas_driver.ini fwaas enabled True

    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types  gre
    crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2_type_gre tunnel_id_ranges  1:1000

    ovs-vsctl add-br br-ex
    ovs-vsctl add-port br-ex $INTERFACE_NAME 
    cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE_NAME <<EOF
    DEVICE=$INTERFACE_NAME
    TYPE=Ethernet
    BOOTPROTO=none
    ONBOOT=yes
EOF
    service_start "network"
    crudini --set  /etc/neutron/l3_agent.ini DEFAULT  external_network_bridge  br-ex
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings  physnet1:br-ex
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini agent tunnel_types  gre
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs local_ip ${HOST_IP}
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs  enable_tunneling True
    crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings external:br-ex

    neutron-db-manage --service lbaas upgrade head
    neutron-db-manage --subproject neutron-fwaas upgrade head

    ser='neutron-server  neutron-l3-agent neutron-openvswitch-agent  neutron-lbaas-agent'
    service_start "$ser"
    echo "Neutron-gre服务安装完成" >> /install_logs.log
}
dashboard_install(){
    service_install "openstack-dashboard"
    crudini --set /etc/openstack-dashboard/local_settings OPENSTACK_HOST OPENSTACK_HOST "${host_name}"
    crudini --set /etc/openstack-dashboard/local_settings OPENSTACK_HOST ALLOWED_HOSTS ['*']
    sed -i 's#^ALLOWED_HOSTS =.*#ALLOWED_HOSTS = ['*', ]#g' /etc/openstack-dashboard/local_settings
    sed -i 's/#OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT.*/OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT = True/g' /etc/openstack-dashboard/local_settings
    sed -i "s/OPENSTACK_KEYSTONE_DEFAULT_DOMAIN =.*/OPENSTACK_KEYSTONE_DEFAULT_DOMAIN = ${DOMAIN_NAME}/g" /etc/openstack-dashboard/local_settings
    sed -i "s/TIME_ZONE*=.*/TIME_ZONE = Asia\/Shanghai/" /etc/openstack-dashboard/local_settings
    sed -i "s/TIME_ZONE =.*/TIME_ZONE = \"Asia\/Shanghai\"/g" /etc/openstack-dashboard/local_settings
    echo -e "SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

CACHES = {
    'default': {
         'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
         'LOCATION': '$host_name:11211',
    }
}" >> /etc/openstack-dashboard/local_settings
    echo "OPENSTACK_API_VERSIONS = {
    "identity": 3,
    "image": 2,
    "volume": 2,
}" >> /etc/openstack-dashboard/local_settings
    ser='httpd.service memcached.service'
    service_start "$ser"
    echo "Dashboard服务安装完成" >> /install_logs.log
    systemctl restart httpd
    if [[ "$?" -eq '0' ]];then
        print_red_z_green_z_info "请打开下面的网站进行登录" "\nhttp://$HOST_IP/dashboard/"
        print_red_z_green_z_info "用户名" "admin"
        print_red_z_green_z_info "密码" "$ALL_PASS"
    else
        print_red_z_b "服务启动失败"
    fi
}
Mariadb_all(){
    chrony_install
    mongodb_install
    rabbitmq_install
    memcached_install
    mariadb_install
    init_mariadb
    database_create
}
install_start(){
    # print_red_z_green_z_info "正在进行" "源仓库配置"
    # yum_config
    # # print_red_z_green_z_info "正在进行" "系统安全配置"
    # # config_sys
    if [[ "$config" == 'y' ]];then
        commands='Mariadb_all keystone_install glance_install nova_install neutron_install neutron_${host_name}_gre dashboard_install'
        input "是否一次性安装全部组件[y/n]" "auto"
        if [[ "$auto" == "y" || "$auto" == "Y" ]];then
            for i in $commands;do
                $i
            done
        else
            command_sec='[1]Mariadb_all [2]keystone_install [3]glance_install [4]nova_install [5]neutron_install [6]neutron_${host_name}_gre [7]dashboard_install'
            for i in $command_sec;do
                print_green_zt "$i"
            done
            input "请从以上列表选择在一个安装功能" "az"
            case "$az" in
                "1") Mariadb_all
                ;;
                "2") keystone_install
                ;;
                "3") glance_install
                ;;
                "4") nova_install
                ;;
                "5") neutron_install
                ;;
                "6") neutron_${host_name}_gre
                ;;
                "7") dashboard_install
                ;;
            esac
        fi
    else
        print_red_z_green_z_info "正在进行" "源仓库配置"
        yum_config
        print_red_z_green_z_info "正在进行" "系统安全配置"
        config_sys
    fi
}

install_info(){
    install_check
    if [[ "$Successs" -eq 'y' || "$Successs" -eq 'Y' ]];then
        input "[1]安装openstackM版\n[待完善]安装openstackQ版\n[x]卸载openstack环境\n请选择>>>" "YY"
        if [[ $YY == '1' ]];then
            install_start
        elif [ $YY == x ];then
            print_red_z_green_z_info "待更新"
        else
            print_red '选择出错'
            sh openstack/Y
            #返回主页
        fi
    else
        echo -e "请先配置$INFO_FILE文件"
    fi
}
install_info